package util.ir;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;

import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;



public class QueryClef {

	
	

	
	public static void standardSearch(String question, String field,
			String index, Hashtable<String, Float> hash, int max) throws ParseException,
			CorruptIndexException, IOException {

		String querystr = question;
		StandardAnalyzer analyzer = new StandardAnalyzer();

		Query q = new QueryParser(field, analyzer).parse(querystr);

	
		IndexSearcher searcher = new IndexSearcher(index);

		// ScoreDoc[] hits = collector.topDocs().scoreDocs;

		
		System.out.println("Searching for: " + q.toString(field));

		Hits hits = searcher.search(q);
		System.out.println("> "); // start with '> '

		int end = Math.min(hits.length(), max);
		for (int i = 0; i < end; i++) {

			Document doc = hits.doc(i);
			String name = doc.get("original");
			if (name != null) {

				
					System.out.println((i + 1) + ". " + name + " (score "
							+ hits.score(i) + ", docid 000000");

				

				if (hash.containsKey(name)) {

					Float f = hash.get(name);
					f = f + hits.score(i);

				} else {

					hash.put(name, hits.score(i));
				}

			} else {
				System.out
						.println((i + 1) + ". " + "No name for this document");
			}
		}

		System.out.println("\n" + hits.length() + " matching documents");

		searcher.close();

	}
	
	
	public static void main(String argsp[]) throws CorruptIndexException, ParseException, IOException{
		
		
		
		String index = "/project/index_temporal.txt";
		Hashtable<String, Float> hash = new Hashtable<String,Float>();
		standardSearch("kids", "indexed", index,hash,10);
		
	}
	
	
}
	    


	
	
	
	
	
